package com.tianquan.mybatis;/**
 * @author starsea
 * @date 2024-09-11 上午11:16
 */

import com.tianquan.mapper.DishMapper;
import org.apache.ibatis.binding.MapperProxyFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.mybatis
 * @Author: tianquan
 * @CreateTime: 2024-09-11  11:16
 * @Description: 测试
 * @Version: 1.0
 */

// @Component
public class MapperFactory implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        SqlSessionFactory sqlSessionFactory = defaultListableBeanFactory.getBean(SqlSessionFactory.class);

        MapperProxyFactory mapperProxyFactory = new MapperProxyFactory(DishMapper.class);
        DishMapper dishMapper = (DishMapper) mapperProxyFactory.newInstance(sqlSessionFactory.openSession());

        // DishMapper dishMapper = sqlSessionFactory.openSession().getMapper(DishMapper.class);
        defaultListableBeanFactory.registerSingleton("dishMapper", dishMapper);
    }

}
